using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class Shop_HotKey
	{
		private Conn conn = null;
		public Shop_HotKey()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public Shop_HotKey(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAllHotKeys()
		{
			return this.conn.GetDataTable("SELECT top 1000 * FROM HLShop_HotKey Order By Hits desc");
		}
		public DataTable GetTop10HotKey()
		{
			return this.conn.GetDataTable("SELECT TOP 10 hits, id, keyName, search_url FROM HLShop_HotKey ORDER BY hits DESC");
		}
		public bool AddHotKey(string keyName, string url)
		{
			string text = "";
			if (keyName != "" && keyName != null)
			{
				text = keyName;
			}
			bool result;
			if (text != "")
			{
				IDbDataParameter dbDataParameter = this.conn.CreateParameter("@KeyName", DbType.String, text.Trim());
				int num = this.conn.ExecuteCount("Select count(id) From [HLShop_HotKey] Where ([keyName] = @KeyName)", new IDbDataParameter[]
				{
					dbDataParameter
				});
				int num2;
				if (num > 0)
				{
					num2 = this.conn.Execute("UPDATE [HLShop_HotKey] Set hits=hits+1 Where ([keyName] = @KeyName)", new IDbDataParameter[]
					{
						dbDataParameter
					});
				}
				else
				{
					IDbDataParameter[] parameters = new IDbDataParameter[]
					{
						this.conn.CreateParameter("@KeyName", DbType.String, text.Trim()),
						this.conn.CreateParameter("@search_url", DbType.String, url.Trim())
					};
					num2 = this.conn.Execute("Insert Into [HLShop_HotKey](keyName,hits,search_url) values(@KeyName,1,@search_url)", parameters);
				}
				result = (num2 > 0);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public DataTable getHotKeyByID(int HotKey)
		{
			return this.conn.GetDataTable("SELECT * FROM HLShop_HotKey WHERE id=" + HotKey);
		}
		public bool AddHotKey(string keyName, string url, int Hits)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@KeyName", DbType.String, keyName.Trim()),
				this.conn.CreateParameter("@search_url", DbType.String, url.Trim()),
				this.conn.CreateParameter("@Hits", DbType.Int32, Hits)
			};
			int num = this.conn.Execute("INSERT INTO [HLShop_HotKey] (keyName,hits,search_url) VALUES (@KeyName,@Hits,@search_url)", parameters);
			return num > 0;
		}
		public bool DeleteHotKey(int ID)
		{
			return ID > 0 && this.conn.Execute("DELETE From [HLShop_HotKey] Where ID=" + ID) == 1;
		}
		public bool UpdateHotKey(int ID, string KeyName, int Hits, string Url)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@KeyName", DbType.String, KeyName.Trim()),
				this.conn.CreateParameter("@search_url", DbType.String, Url.Trim()),
				this.conn.CreateParameter("@Hits", DbType.Int32, Hits),
				this.conn.CreateParameter("@ID", DbType.Int32, ID)
			};
			int num = this.conn.Execute("UPDATE [HLShop_HotKey] Set [hits] = @Hits,[search_url] = @search_url,[keyName] = @KeyName Where ([ID] = @ID)", parameters);
			return num == 1;
		}
		public bool UpdateHotKey(string KeyName, int Hits, bool AddHits)
		{
			string str;
			if (AddHits)
			{
				str = "[hits] = [hits]+" + Hits;
			}
			else
			{
				str = "[hits] = " + Hits;
			}
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@KeyName", DbType.String, KeyName.Trim());
			int num = this.conn.Execute("UPDATE [HLShop_HotKey] Set " + str + " Where ([keyName] = @KeyName)", new IDbDataParameter[]
			{
				dbDataParameter
			});
			return num == 1;
		}
		public bool deleteByConnition(int hits)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@hits", DbType.Int32, hits)
			};
			int num = this.conn.Execute("DELETE FROM [HLShop_HotKey] WHERE hits < @hits", parameters);
			return num > 0;
		}
	}
}
